
rem
bbdoc: Simple Newton physics component.
about: Manages acceleration, velocity, friction.
end rem
Type TPhysicsComponent Extends TComponent {expose hidefields}

	Field _velocity:TVector2D
	Field _acceleration:TVector2D
	
	'friction applied each update.
	Field _friction:Float
	
		
	Method New() {hidden}
		_velocity = New TVector2D
		_acceleration = New TVector2D
		_friction = 0.0
	End Method
	
	
	Rem
	bbdoc: Constructor.
	endrem
	Function Create:TPhysicsComponent(friction:Float = 0.0) {hidden}
		Local p:TPhysicsComponent = New TPhysicsComponent
		p._friction = friction
		Return p
	End Function
	
		
	Rem
	bbdoc: Sets the acceleration value.
	about: Specified amount is the initial acceleration.
	endrem	
	Method SetAcceleration(angle:Float, amount:Float = 1.0)
		_acceleration.SetX(Sin(angle))
		_acceleration.SetY(Cos(angle))
		_acceleration.Multiply(amount)
	End Method
	
		
	Rem
	bbdoc: Stops acceleration.
	endrem
	Method ResetAcceleration()
		_acceleration.Set(0, 0)
	End Method
		
	
	Rem
	bbdoc: Adds specified amount to acceleration
	endrem
	Method AddAcceleration(angle:Float, amount:Float = 1)
		_acceleration.Add(Sin(angle) * amount, -Cos(angle)* amount)
	End Method
	
		
	Rem
	bbdoc: Returns current acceleration.
	endrem
	Method GetAcceleration:TVector2D()
		Return _acceleration
	End Method
		
	
	Rem
	bbdoc: Sets friction.
	endrem	
	Method SetFriction(f:Float)
		_friction = f
	End Method

	
	Rem
	bbdoc: Returns friction.
	endrem
	Method GetFriction:Float()
		Return _friction
	End Method	
	
	
	Rem
	bbdoc: Sets velocity.
	endrem
	Method SetVelocity(angle:Float, amount:Float = 1.0)
		_acceleration.Set(Sin(angle) * amount, -Cos(angle)* amount)
	End Method
	
		
	Rem
	bbdoc: Sets velocity by specified vector.
	endrem	
	Method SetVelocityV(v:TVector2D)
		_velocity.Set(v.GetX(), v.GetY())
	End Method
	
		
	Rem
	bbdoc: Returns velocity.
	endrem	
	Method GetVelocity:TVector2D()
		Return _velocity
	End Method
	
End Type
